package com.mxt.moviedemo.controller;

import com.mxt.moviedemo.entity.Movies;
import com.mxt.moviedemo.entity.Ticket;
import com.mxt.moviedemo.service.TicketService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("ticket")
public class TicketController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private TicketService ticketService;
    @GetMapping("/searchTicket")
    public Map<String, Object> searchTicket(@RequestParam("uname") String uname) {
        logger.info("获取购票信息，id：{}", uname);
        Map<String, Object> result = new HashMap<>();

        try {
            List<Ticket> tickets=ticketService.searchTicketsByName(uname);
            if (tickets != null) {
                result.put("success", true);
                result.put("tickets", tickets);
            } else {
                result.put("success", false);
                result.put("error", "未找到该购票信息");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", "查询购票信息失败：" + e.getMessage());
        }
        return result;
    }
    @GetMapping("/infoTicket")
    public Map<String, Object> getUserInfo(@RequestParam("tid") int tid) {
        logger.info("获取购票信息信息，id：{}", tid);
        Map<String, Object> result = new HashMap<>();

        try {
            Ticket ticket=ticketService.getTicketById(tid);
            if (ticket != null) {
                result.put("success", true);
                result.put("ticket", ticket);
            } else {
                result.put("success", false);
                result.put("error", "未找到该订单");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", "查询购票信息失败：" + e.getMessage());
        }
        return result;
    }

    // 新增订票
    @PostMapping("addTicket")
    public Map<String, Object> addTicket(@RequestBody Ticket ticket) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 计算总价
            double price = Double.parseDouble(ticket.getPrice());
            int count = ticket.getCount();
            double totalPrice = price * count;
            ticket.setTotalprice(String.valueOf(totalPrice));

            // 调试信息
            System.out.println("接收到的总价: " + ticket.getTotalprice());

            int rows = ticketService.addTicket(ticket);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "购票添加成功");
            } else {
                result.put("success", false);
                result.put("message", "购票添加失败");
            }
        } catch (Exception e) {
            logger.error("添加购票错误: ", e);
            result.put("success", false);
            result.put("message", "购票添加失败：" + e.getMessage());
        }
        return result;
    }

    @PutMapping("updateTicket")
    public Map<String, Object> updateMovie(@RequestBody Ticket ticket) {
        Map<String, Object> result = new HashMap<>();
        try {
            double price = Double.parseDouble(ticket.getPrice());
            int count = ticket.getCount();
            double totalPrice = price * count;
            ticket.setTotalprice(String.valueOf(totalPrice));
            System.out.println("我要肘击了");
            System.out.println(ticket.getUuid());
            if (ticketService.updateTicket(ticket)) {
                result.put("success", true);
                result.put("message", "购票信息修改成功");
            } else {
                result.put("success", false);
                result.put("message", "购票信息修改失败");
            }
        } catch (Exception e) {
            logger.error("购票信息错误: ", e);
            result.put("success", false);
            result.put("message", "购票信息修改失败：" + e.getMessage());
        }
        return result;
    }
    @DeleteMapping("deleteTicket/{tid}")
    public Map<String, Object> deleteTicket(@PathVariable int tid) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = ticketService.deleteTicket(tid);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "购票信息删除成功");
            } else {
                result.put("success", false);
                result.put("message", "购票信息删除失败");
            }
        } catch (Exception e) {
            logger.error("删除购票信息错误: ", e);
            result.put("success", false);
            result.put("message", "购票信息删除失败：" + e.getMessage());
        }
        return result;
    }

}